import service from '../utils/request';

const getUserInfoApi = async () => {
  try {
    const response = await service.get('/api/user/info');
    return response;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

const changePasswordApi = async (passwordForm) => {
  try {
    const response = await service.put('/api/user/update/password', passwordForm);
    return response;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

const sendCodeApi = async (params) => {
  try {
    const response = await service.post('/api/user/update/code', params);
    return response;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

//用户添加problem
const saveProblemApi = async (problem) => {
  try {
    const response = await service.post('/api/user/problem',problem);
    return response;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

//用户查询problem
const getProblemApi = async () => {
  try {
    const response = await service.get('/api/user/problem');
    return response;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

//用户更新problem
const updateProblemApi = async (problem) => {
  try {
    const response = await service.put('/api/user/problem', problem);
    return response;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

//用户删除problem
const deleteProblemApi = async (id) => {
  try {
    const response = await service.delete(`/api/user/problem/${id}`);
    return response;
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

// 用户上传文件
const uploadFileApi = async (file) => {
  try {
    const formData = new FormData()
    formData.append('file', file)
    const response = await service.post('/api/user/upload', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
    return response
  } catch (error) {
    console.error('Error:', error)
    throw error
  }
}

// 根据图片 id 查询文件 url
const getPictureUrlApi = async (ids) => {
  try {
    const response = await service.get('/api/user/picture', {
      params: { ids: ids.join(',') }
    })
    return response
  } catch (error) {
    console.error('Error:', error)
    throw error
  }
}

export { getUserInfoApi ,changePasswordApi ,sendCodeApi, saveProblemApi, getProblemApi, updateProblemApi, deleteProblemApi, uploadFileApi, getPictureUrlApi};